// pages/home/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    type:'热门推荐',
    user:'{"tx":"https://thirdwx.qlogo.cn/mmopen/vi_32/vYOPewuv6LjvPibudDrrB9dRDUwGqV7qfoibIXGrwyHNwVgribzibJaFf18YBkGicqNQaZa8WceoWMNzZJeCcq86EXg/132","name":"『Silence』&#128347;"}',
    // 轮播图数据
    bannerList:[],
    // swiper
    recommendList:[
      {
        picUrl:'../../img/wlk/home_food1.png',
        name:'速食锅',
        type:'home_tabs2_one_block'
      },
      {
        picUrl:'../../img/wlk/home_food2.png',
        name:'家常锅',
        type:'home_tabs2_two_block'
      },
      {
        picUrl:'../../img/wlk/home_food3.png',
        name:'早餐锅',
        type:'home_tabs2_three_block'
      },
      {
        picUrl:'../../img/wlk/home_food4.png',
        name:'药膳锅',
        type:'home_tabs2_four_block'
      },
      {
        picUrl:'../../img/wlk/home_food5.png',
        name:'美味锅',
        type:'home_tabs2_five_block'
      },
      {
        picUrl:'../../img/wlk/home_food6.png',
        name:'健康锅',
        type:'home_tabs2_six_block'
      },
      {
        picUrl:'../../img/wlk/home_food2.png',
        name:'下饭锅',
        type:'home_tabs2_seven_block'
      }
    ],
    // 厨房宝典
    kitchenList:['../../img/wlk/home_kitchen1.png','../../img/wlk/home_kitchen2.png','../../img/wlk/home_kitchen3.png'],
    curr:'home_tabs1_one_block',
    curr2:'home_tabs2_one_block',
    // 热门推荐左边
    hotLeft:[],
    // 热门推荐右边
    hotRight:[],
    // 速食锅
    fastList:[],
    // 家常锅
    dailyList:[],
    // 早餐锅
    breakfastList:[],
    // 药膳锅
    medicinalList:[],
    // 美味锅
    deliciousList:[],
    // 健康锅
    healthList:[],
    // 下饭锅
    ourList:[],
  },
  // 请求热门推荐数据
  getRecommendList(){
    let that=this;
    let {hotRight,hotLeft} = that.data;
    wx.request({
      url: 'http://localhost:8888/getZp',
      success:(res)=>{
        // console.log(res.data.data)
        res.data.data=res.data.data.filter((item,index)=>{
          item.userid=JSON.parse(item.userid)
          item.img=JSON.parse(item.img)
          if(item.watch==null){
            item.watch=0
          }
          if(index%2==0){
            hotRight.push(item)
            that.setData({
              hotRight
            })
          }else{
            hotLeft.push(item)
            that.setData({
              hotLeft
            })
          }
          return item
        })
      }
    })
  },
  // 请求数据
  geList(){
    let that=this;
    wx.request({
      url: 'http://localhost:8888/homeData',
      success:(res)=>{
        // console.log(res.data.data)
        let list1 =  [];
        let list2 =  [];
        let list3 =  [];
        let list4 =  [];
        let list5 =  [];
        let list6 =  [];
        let list7 =  [];
        let list8 =  [];
        res.data.data.filter((item)=>{
          if(item.likes){
            item.likes=JSON.parse(item.likes)
          }else{
            item.likes=[]
          }
          let is=false
          is = item.likes.filter((item)=>item.name==JSON.parse(that.data.user).name).length>0?true:false;
          item.islike = item.likes?is?true:false:false
          // 轮播图
          if(item.classify==='轮播'){
            list1.push(item)
            that.setData({
              bannerList:list1,
            })
          }
          // 速食锅
          if(item.classify==="速食锅"){
            list2.push(item)
            that.setData({
              fastList:list2,
            })
          }
          // 家常锅
          if(item.classify==="家常锅"){
            list3.push(item)
            that.setData({
              dailyList:list3,
            })
          }
          // 早餐锅
          if(item.classify==="早餐锅"){
            list4.push(item)
            that.setData({
              breakfastList:list4,
            })
          }
          // 药膳锅
           if(item.classify==="药膳锅"){
            list5.push(item)
            that.setData({
              medicinalList:list5,
            })
          }
          // 美味锅
          if(item.classify==="美味锅"){
            list6.push(item)
            that.setData({
              deliciousList:list6,
            })
          }
          // 健康锅
          if(item.classify==="健康锅"){
            list7.push(item)
            that.setData({
              healthList:list7,
            })
          }
          // 下饭锅
          if(item.classify==="下饭锅"){
            list8.push(item)
            that.setData({
              ourList:list8,
            })
          }
        })
      }
    })
  },
  //爱心
  editLikes(e){
    let {id,likes,islike}=e.currentTarget.dataset.item;
    let _this=this
    if(islike){
      let ind = ''
      likes.filter((item,index)=>{
        if(item.name == JSON.parse(_this.data.user).name){
          ind = index
        }
      })
      likes.splice(ind,1)
    }else{
      likes.push(JSON.parse(_this.data.user))
    }
    likes = JSON.stringify(likes)
    wx.request({
      url: 'http://localhost:8888/editlikes',
      method:'POST',
      data:{id,likes},
      success:(res)=>{
        if(res.data.code==200){
          _this.geList()
        }
      }
    })
  },
  // 选项卡1
  changeFu(e){
    let curr=e.currentTarget.dataset.curr
    if(e.currentTarget.dataset.index){
      let index=e.currentTarget.dataset.index
      let curr2=this.data.recommendList[index].type
      this.setData({
        curr2,
      })
    }
    this.setData({
      curr:curr,
    })
  },
  // 选项卡2
  changeFu2(e){
    let curr2=e.target.dataset.curr
    this.setData({
      curr2:curr2
    })
  },
  // 搜索页面
  toSearch(){
    wx.navigateTo({
      url: '/pages/search/search',
    })
  },
  // 观看量
  toWatch(e){
    let {id,watch}=e.currentTarget.dataset.item;
    let _this=this
    watch+=1
    wx.request({
      url: 'http://localhost:8888/editwatch',
      method:'POST',
      data:{id,watch},
      success:(res)=>{
        if(res.data.code==200){
          _this.geList()
          wx.navigateTo({
            url: '/pages/details/details?id='+id,
          })
        }
      }
    })
  },
  toWatches(e){
    console.log(111,e)
    let {id,watch}=e.currentTarget.dataset.item;
    let _this=this;
    let {type} = _this.data;
    watch+=1
    console.log(watch)
    wx.request({
      url: 'http://localhost:8888/editcheck',
      method:'POST',
      data:{id,watch},
      success:(res)=>{
        console.log(res.data)
        if(res.data.code==200){
          _this.setData({
            hotLeft:[],
            hotRight:[]
          })
          _this.getRecommendList()
          wx.navigateTo({
            url: '/pages/details/details?type='+type+'&id2='+id,
          })
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      user:wx.getStorageSync('user')
    })
    this.geList()
    this.getRecommendList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})